package com.xinxin.blockchain.api.controller;

import com.xinxin.base.controller.BaseController;
import com.xinxin.base.entity.ApiResult;
import com.xinxin.blockchain.dto.SmsCodeDTO;
import com.xinxin.blockchain.service.ICommonService;
import com.xinxin.blockchain.vo.CountryCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author ys
 * @date 2021/5/20
 */
@Api(tags = "公共接口")
@Validated
@RestController
@RequestMapping("common")
public class CommonController extends BaseController {

    @Autowired
    private ICommonService commonService;

    @ApiOperation("国家号码列表")
    @GetMapping("country-code")
    public ApiResult<List<CountryCodeVO>> countryCode() {
        return ApiResult.success(commonService.countryCode());
    }

    @ApiOperation("发送短信验证码")
    @PostMapping("code/sms")
    public ApiResult sendSmsCode(@Valid @ModelAttribute SmsCodeDTO dto) {
        commonService.sendSmsCode(dto.getCountryCode(), dto.getMobile(), dto.getType());
        return ApiResult.success();
    }

    @ApiOperation("刷新账户Token")
    @PostMapping("token/refresh")
    public ApiResult<String> refreshToken(@NotBlank @RequestParam String refreshToken) {
        return ApiResult.success(commonService.refreshToken(refreshToken));
    }
}
